<template>
  <div id="app">
    <a-config-provider :locale="locale">
      <router-view />
    </a-config-provider>

    <BackTop />
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { throttle } from "lodash";
import BackTop from "./components/BackTop";
import { isLogin } from "./utils/auth";

export default {
  components: {
    BackTop
  },
  data() {
    return {
      scrollTop: 0
    };
  },
  provide() {
    return {
      app: this
    };
  },
  computed: {
    ...mapGetters(["lang", "account"]),
    locale() {
      return this.$i18n.getLocaleMessage(this.lang).message.ant;
    }
  },
  methods: {
    openAuthNotification() {
      this.$notification.open({
        message: "温馨提示",
        key: "auth",
        description: "该账号还未进行认证，请进行认证解锁更多功能",
        icon: (
          <a-icon type="info-circle" theme="filled" style="color: #52C41A;" />
        ),
        btn: (
          <a-button
            type="primary"
            onClick={() => {
              this.$router.push("/auth/home");
              this.$notification.close("auth");
            }}
          >
            去认证
          </a-button>
        ),
        duration: 5
      });
    },
    checkAuth(checkAuth = true) {
      if (!isLogin()) {
        this.openLoginModal();
        return false;
      }

      if (this.account.status !== 10 && checkAuth) {
        this.openAuthNotification();
        return false;
      }

      return true;
    },
    openLoginModal() {
      this.$refs.loginModal.open();
    }
  },
  mounted() {
    this.$bus.$on("LOGIN_MODAL", () => {
      this.openLoginModal();
    });

    this.$bus.$on("OPEN_AUTH_NOTIFICATION", () => {
      this.openAuthNotification();
    });

    const doc = document;

    // scroll top
    doc.addEventListener(
      "scroll",
      throttle(() => {
        this.scrollTop = doc.documentElement.scrollTop || doc.body.scrollTop;
      }, 50)
    );

    window.addEventListener("beforeunload", () => {
      if (this.$route.meta.beforeClose && window.IS_EDIT) {
        window.event.returnValue = "您有未保存的数据，确认要离开吗";
      }
    });

    window.addEventListener("change", () => {
      window.IS_EDIT = true;
    });
  }
};
</script>
